package com.vitaf.cash.app.databinding

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class UserDataBindingAdapter(private val userList: List<GenericObservableModel>) : RecyclerView.Adapter<UserDataBindingAdapter.UserViewHolder>() {

    // 创建 ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val binding = ActDatabindingUserItemBinding.inflate(LayoutInflater.from(parent.context),parent, false)
        return UserViewHolder(binding)
    }

    // 绑定数据
    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.bind(userList[position])
    }

    override fun getItemCount(): Int = userList.size

    // ViewHolder 类
    inner class UserViewHolder(private val binding: ActDatabindingUserItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(user: GenericObservableModel) {
            binding.model = user
            binding.executePendingBindings() // 立即执行绑定
        }
    }
}